import { InstanceIdStruct } from "../../../../../Core/Module/IdGenerater/IdGenerater";
import { Log } from "../../../../../Core/Module/Log/Log";
import { ExpandOf } from "../../../../../Decorator/Decorator";
import { StartMachineConfig, StartMachineConfigCategory } from "../Config/StartMachineConfig";
import { StartProcessConfig } from "../Config/StartProcessConfig";

/**
 * 使用 Expand 扩展方法
 * 
 * 必须作出以下申明，才能调用对象的扩展方法
 */
declare module "../Config/StartProcessConfig" {

    interface StartProcessConfig {

        SceneId: number;

        readonly InnerIPPort: string;

        readonly InnerIP: string;

        readonly OuterIP: string;

        readonly StartMachineConfig: StartMachineConfig;

    }

}

@ExpandOf(StartProcessConfig)
export class StartProcessConfigExpand {

    private static AfterEndInit(self: StartProcessConfig) {
        const instanceIdStruct = InstanceIdStruct.CreateWith(self.Id, 0);
        self.SceneId = instanceIdStruct.ToLong();

        Log.Info(`StartProcess info: ${self.MachineId} ${self.Id} ${self.SceneId}`);
    }

}

Object.defineProperty(StartProcessConfig.prototype, "InnerIPPort", {
    get() {
        return `${this.InnerIP}:${this.InnerPort}`;
    }
});

Object.defineProperty(StartProcessConfig.prototype, "InnerIP", {
    get() {
        return this.StartMachineConfig.InnerIP;
    }
});

Object.defineProperty(StartProcessConfig.prototype, "OuterIP", {
    get() {
        return this.StartMachineConfig.OuterIP;
    }
});

Object.defineProperty(StartProcessConfig.prototype, "StartMachineConfig", {
    get() {
        return StartMachineConfigCategory.Instance.Get(this.MachineId);
    }
});